import express from 'express';
import xcxUserModel from "../../../mongo/models/xcxUser"
import { jwtSign } from "../../../utils/jwt"
import axios from "axios"
const router = express.Router();

// const secret = "938238bcf159a657fa1fc654641b6482"
// const appid = "wx40da3dee5cf1b352"

// yz
const secret = "0360bb7b27b3bb14b0790b3e71761b36"
const appid = "wx93b1ce761fb7196a"

// 小程序登录
router.post("/xcx/login", (req: any, res) => {
    const { js_code } = req.body;

    if (!js_code) {
        return res.send({ code: 1, msg: "参数错误" })
    }

    axios.get(`https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${js_code}&grant_type=authorization_code`).then((info: any) => {
        if (info.data.errcode) return res.send({ code: 400, msg: "登录失败" })
        const token = jwtSign(info.data)
        // 查询数据
        xcxUserModel.findOne({ openid: info.data.openid }).then((data: any) => {
            if (data) {
                return res.send({ code: 200, msg: token })
            }
            xcxUserModel.create({
                openid: info.data.openid,
                session_key: info.data.session_key,
            })
            res.send({ code: 200, msg: token })
        }).catch((err: any) => {
            res.send({ code: 400, msg: "登录失败" })
        })
    }).catch((err: any) => {
        res.send({ code: 403, msg: "登录失败" })
    })
})

// 写入头像
router.post("/xcx/avatar", (req: any, res) => {
    const { openid } = req.jwtInfo;
    console.log(req.jwtInfo);
    const { avatar } = req.body;
    if (!openid || !avatar) {
        return res.send({ code: 400, msg: "参数错误" })
    }
    xcxUserModel.updateOne({ openid }, { avatar }).then((data: any) => {
        res.send({ code: 200, msg: "头像上传成功" })
    }).catch((err: any) => {
        res.send({ code: 400, msg: "头像上传失败" })
    })
})



// 写入手机号
router.post('/xcx/phone', (req: any, res) => {
    const { encryptedData, iv } = req.body;

    console.log(encryptedData, iv);


    if (!encryptedData || !iv) {
        return res.send({ code: 400, msg: "参数错误" })
    }

    axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`)
        .then((infoAccess: any) => {
            const { access_token } = infoAccess.data;
            // 解密手机号
            axios.post(`https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${access_token}`, {
                code: encryptedData,
            }, {
                "headers": {
                    "Accept": "application/json",
                    'Content-type': "application/json"
                }
            }).then((info: any) => {
                console.log(info.data);
                if (info.data.errcode) {
                    return res.send({ code: 400, msg: "手机号绑定失败1" })
                }
                const { phoneNumber, purePhoneNumber } = info.data;
                // 写入号码
                xcxUserModel.updateOne({ openid: req.jwtInfo.openid }, { phone: phoneNumber }).then((data: any) => {
                    res.send({ code: 200, msg: "手机号绑定成功" })
                }).catch((err: any) => {
                    res.send({ code: 400, msg: "手机号绑定失败2" })
                })
            }).catch((err: any) => {
                res.send({ code: 400, msg: "手机号绑定失败3" })
            })
        }).catch(() => {
            res.send({ code: 400, msg: "手机号绑定失败4" })
        })

});


export default router; 